"""
Terminal终端版聊天室
"""
import sys

from chat_scanner import ChatScanner
from chat_server import ChatServer
from chat_client import ChatClient
from utils import *

main_tip = """====================================
【 1 -> 查看所有聊天室 】
【 2 -> 创建聊天室 】
【 q -> 退出 】
====================================
"""


def start_chatroom_console():

    while True:
        clear_console()
        print(main_tip)
        choice = input('请输入选项：')
        if choice == 'q':
            print("退出程序")
            break
        elif choice == '1':
            # 查看聊天室&进入聊天室
            scanner = ChatScanner()
            room = scanner.list()
            print(room)
            if room is None:
                continue
            print()
            nickname = input(f"您将进入【{room['name']}】，请输入您的昵称：")
            # 聊天室客户端
            client = ChatClient(nickname)
            client.enter(room)
        elif choice == '2':
            # 创建聊天室
            name = input('请输入聊天室名称：')
            chatroom = ChatServer(name)
            chatroom.run()
        else:
            print('无效的选项！')


if __name__ == '__main__':
    try:
        start_chatroom_console()
    except KeyboardInterrupt as e:
        print("用户Ctrl + C退出")
        sys.exit(0)
